<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

	<link href="../../../static/font-awesome-4.7.0/css/font-awesome.min.css" rel="stylesheet">
	<link rel="stylesheet" href="../../../static/vui/css/vui.css">

	<script type="text/javascript" src="../../../static/js/jquery-1.9.1.min.js"></script>
	<script type="text/javascript" src="../../../static/js/jquery.cookie.min.js"></script>
	<script src="../../../static/layer/layer.js"></script>
	<script type="text/javascript" src="../../../static/vui/js/vui.js"></script>
	<script type="text/javascript" src="../../../static/js/common.js"></script>
</head>
<body>

	<div id="formArea">

		<table class="table">
			<tr id="parentTr">
				<td>
					<label class="label">上级分类</label>
				</td>
				<td>
					<input type="text" class="input" readonly="readonly" name="parentId_text"  autocomplete="off"/>
					<input type="hidden" name="parentId"/>
				</td>
			</tr>
			<tr>
				<td>
					<label class="label required">分类名称</label>
				</td>
				<td>
					<input type="text" class="input" name="name"  autocomplete="off"/>
				</td>
			</tr>
		</table>

	</div>

<script>

var requestParams = app.getRequestParams();
$(function () {
	if(requestParams.id){
        $('#formArea').append('<input type="hidden" value="'+requestParams.id+'" name="id"/>');
        var lastIndex = requestParams.name.lastIndexOf('/');
        if( lastIndex == -1 ){
            $('input[name="parentId_text"]').val('');
            $('input[name="name"]').val(requestParams.name);
		}else{
            $('input[name="parentId_text"]').val(requestParams.name.substring(0,lastIndex));
            $('input[name="name"]').val(requestParams.name.substring(lastIndex+1,requestParams.name.length));
		}

        $('input[name="parentId"]').val(requestParams.parentId);
	}else if(requestParams.parentName){
        $('input[name="parentId_text"]').val(requestParams.parentName);
        $('input[name="parentId"]').val(requestParams.parentId);
	}
});

app.getOkBtn().click(function(){
	if( !checkForm() ){
		return;
	}

	var url = 'productType/insert';

	if(requestParams.id){
		url = 'productType/update'
	}

	var parentName = $('input[name="parentId_text"]').val();
	var data = app.serializeForm();
	if( parentName != null && $.trim(parentName).length>0 ){
        data.name = parentName+'/'+data.name;
	}
    data.companyId = app.getCompanyId();
	app.request({
		url:url,
		data:data,
		success:function (resp) {
            if( resp.result==0 ){
                app.closeLastWindow();
                app.message(parent.layer,'操作成功');
                parent.$("#table").treegrid("reload");
            }else{
                app.message(parent.layer,'操作失败【'+app.getErrorMsg(resp)+'】',2);
            }
        }
	});


});

function checkForm(){

	var checkOptions = [
		{type:'input',name:'name',verify:[{type:'required'}]}
		];

	return app.checkForm(checkOptions);
}

</script>

</body>
</html>